package utilities;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class PdfUtil {

	public static void addMetaData(Document document) {
		document.addTitle(FileConfig.getValue(Constants.DEVP_ENV,
				Constants.TITLE));
		document.addSubject(FileConfig.getValue(Constants.DEVP_ENV,
				Constants.SUBJECT));
		document.addKeywords(FileConfig.getValue(Constants.DEVP_ENV,
				Constants.KEYWORD));
		document.addAuthor(FileConfig.getValue(Constants.DEVP_ENV,
				Constants.AUTHOR));
		document.addCreator(FileConfig.getValue(Constants.DEVP_ENV,
				Constants.CREATOR));
		document.setPageSize(PageSize.LEGAL);
	}

	public static PdfPTable addCharge(String t1) throws DocumentException {

		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100);
		table.setWidths(new int[] { 40, 1 });
		// sel 1
		Paragraph p = new Paragraph();
		Chunk ch = new Chunk(t1, FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_E, Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		PdfPCell c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setPadding(1);
		c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		c1.setVerticalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);

		// sel 1
		p = new Paragraph();
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setPadding(1);
		c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		table.addCell(c1);

		return table;

	}

	public static PdfPTable addStatement(String t1) throws DocumentException {

		PdfPTable table = new PdfPTable(2);
		table.setWidthPercentage(100);
		table.setWidths(new int[] { 40, 1 });
		// sel 1
		Paragraph p = new Paragraph();
		Chunk ch = new Chunk(t1, FontFactory.getFont(FontFactory.HELVETICA,
				Constants.FONT_E, Font.NORMAL, BaseColor.BLACK));
		p.add(ch);
		PdfPCell c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setPadding(1);
		c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		c1.setVerticalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);

		// sel 1
		p = new Paragraph();
		c1 = new PdfPCell(p);
		c1.setBorderWidth(0);
		c1.setPadding(1);
		c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
		c1.setVerticalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);

		return table;

	}

	public static String padRight(String s, int n) {
		return String.format("%-" + n + "s", s);
	}

	public static String padLeft(String s, int n) {
		return String.format("%" + n + "s", s);
	}

	public static String addEmptyString(String s, int n) {
		if (n > 0) {
			return s + String.format("%" + n + "s", " ");
		} else {
			return String.format("%" + n + "s", " ") + s;
		}

	}

	public static String addString(String s, float n) {
		// return String.format("%" + n + "s", s).replace(' ', '_');
		if (n > 0) {
			return s + String.format("%" + (n-s.length()) + "s", " ").replace(' ', '_');
		} else {
			return String.format("%" + (n+s.length()) + "s", " ").replace(' ', '_') + s;
		}
	}


	
	public static void main(String[] args) {
		//System.out.println("hello '" + addEmptyString("123", 5) + "'");
		//System.out.println("hello '" + addEmptyString("1234", -1) + "'");
		System.out.println(addString(" Time Based ", 20.5f));
		System.out.println(addString(" AHA Fantastis Pro", 20f));
//		System.out.println(String.format("%10s", "howto").replace(' ', '*'));
//		System.out.println(String.format("%-10s", "howto").replace(' ', '*'));
	}
}
